<template>
  <div class="family-elder-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>我家老人信息</span>
        </div>
      </template>
      <div v-if="loading" class="loading-container">
        <el-skeleton :rows="5" animated />
      </div>
      <div v-else-if="elder" class="elder-info">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="性别">{{ elder.gender === 1 ? '男' : '女' }}</el-descriptions-item> <!-- 统一枚举 -->
          <!-- 其他描述项不变，确保bedId对应后端字段（原bedNumber改为bedId） -->
          <el-descriptions-item label="床位号">{{ elder.bedId }}</el-descriptions-item>
        </el-descriptions>
        <div class="action-buttons" style="margin-top: 20px;">
          <el-button type="primary" @click="viewHealthRecords">查看健康档案</el-button>
          <el-button type="success" @click="viewMealRecords">查看餐饮记录</el-button>
          <el-button type="warning" @click="viewAccidentRecords">查看事故记录</el-button>
          <el-button type="info" @click="sendMessageToCaregiver">联系护工</el-button>
        </div>
      </div>
      <div v-else class="no-data">
        <el-empty description="暂无关联的老人信息" />
      </div>
    </el-card>
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { familyApi } from '@/api/family' // 修复：用家属专属接口
import { useUserStore } from '@/store/user' // 新增：获取当前家属ID

const router = useRouter()
const loading = ref(false)
const elder = ref(null)
const userStore = useUserStore()

// 修复：添加检查确保userInfo存在后再访问familyId
const currentFamilyId = userStore.userInfo?.familyId

// 状态映射不变
const getStatusType = (status) => {
  const statusMap = { 'NORMAL': 'success', 'OUTING': 'warning', 'HOSPITAL': 'danger', 'LEAVE': 'info' }
  return statusMap[status] || 'info'
}
const getStatusText = (status) => {
  const statusTextMap = { 'NORMAL': '在院', 'OUTING': '外出', 'HOSPITAL': '住院', 'LEAVE': '请假' }
  return statusTextMap[status] || '未知状态'
}

// 修复：家属仅查自己关联的老人（传familyId过滤）
const loadElderInfo = async () => {
  // 修复：添加检查确保currentFamilyId存在
  if (!currentFamilyId) {
    ElMessage.error('用户信息不完整，请重新登录')
    return
  }
  
  loading.value = true
  try {
    // 家属接口：根据家属ID获取关联老人
    const response = await familyApi.getFamilyElder({ familyId: currentFamilyId })
    if (response.code === 200 && response.data) {
      elder.value = response.data
    } else {
      ElMessage.warning('暂无关联的老人信息')
    }
  } catch (error) {
    console.error('加载老人信息失败:', error)
    ElMessage.error('加载老人信息失败')
  } finally {
    loading.value = false
  }
}

// 跳转逻辑不变
const viewHealthRecords = () => { router.push('/dashboard/family/medical') }
const viewMealRecords = () => { router.push('/dashboard/family/meal') }
const viewAccidentRecords = () => { router.push('/dashboard/family/accident') }
const sendMessageToCaregiver = () => { router.push('/dashboard/family/messages') }

onMounted(() => {
  loadElderInfo()
})
</script>
<style scoped>
/* 样式不变 */
</style>